package com.example.administrator.szb.activity.myyewu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.URLAddress;

import java.util.HashMap;
import java.util.Iterator;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/10/10.
 */

public class YWGLNOActivity extends BaseActivity {
    @Bind(R.id.ywgl_no_image_fh)
    ImageView ywglNoImageFh;
    @Bind(R.id.ywgl_no_ll_gpzy)
    LinearLayout ywglNoLlGpzy;
    @Bind(R.id.ywgl_no_ll_dzjc)
    LinearLayout ywglNoLlDzjc;
    @Bind(R.id.ywgl_no_ll_qylc)
    LinearLayout ywglNoLlQylc;
    @Bind(R.id.ywgl_no_ll_xydk)
    LinearLayout ywglNoLlXydk;
    @Bind(R.id.ywgl_no_ll_gqdr)
    LinearLayout ywglNoLlGqdr;
    @Bind(R.id.ywgl_no_ll_sbzr)
    LinearLayout ywglNoLlSbzr;
    @Bind(R.id.ywgl_no_ll_bgzr)
    LinearLayout ywglNoLlBgzr;
    @Bind(R.id.ywgl_no_ll_dxzf)
    LinearLayout ywglNoLlDxzf;
    @Bind(R.id.ywgl_no_ll_ggpz)
    LinearLayout ywglNoLlGgpz;
    @Bind(R.id.ywgl_no_checkbox_gpzz)
    CheckBox ywglNoCheckboxGpzz;
    @Bind(R.id.ywgl_no_checkbox_fhbs)
    CheckBox ywglNoCheckboxFhbs;
    @Bind(R.id.ywgl_no_checkbox_kjhz)
    CheckBox ywglNoCheckboxKjhz;
    @Bind(R.id.ywgl_no_checkbox_szgl)
    CheckBox ywglNoCheckboxSzgl;
    @Bind(R.id.ywgl_no_button_tj)
    Button ywglNoButtonTj;
    @Bind(R.id.ywgl_no_text_gpzy)
    TextView ywglNoTextGpzy;
    @Bind(R.id.ywgl_no_text_dzjc)
    TextView ywglNoTextDzjc;
    @Bind(R.id.ywgl_no_text_qylc)
    TextView ywglNoTextQylc;
    @Bind(R.id.ywgl_no_text_xydk)
    TextView ywglNoTextXydk;
    @Bind(R.id.ywgl_no_text_gqdr)
    TextView ywglNoTextGqdr;
    @Bind(R.id.ywgl_no_text_sbzr)
    TextView ywglNoTextSbzr;
    @Bind(R.id.ywgl_no_text_bgzr)
    TextView ywglNoTextBgzr;
    @Bind(R.id.ywgl_no_text_dxzf)
    TextView ywglNoTextDxzf;
    @Bind(R.id.ywgl_no_text_ggpz)
    TextView ywglNoTextGgpz;

    public static HashMap<String,Integer> isVisiable = new HashMap<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ywgl_no_activity);
        ButterKnife.bind(this);
        SampleApplicationLike.getActivitiesInstance().add(this);


    }

    private void hasIntent() {
        Iterator<String> it = isVisiable.keySet().iterator();
        while (it.hasNext()){
            String title = it.next();
            if (isVisiable.get(title) == 1){
                showYBC(title);
            }
        }


    }

    private void showYBC(String title) {
        if (title.equals("股票质押")) {
            ywglNoTextGpzy.setVisibility(View.VISIBLE);
        } else if (title.equals("大宗减持")) {
            ywglNoTextDzjc.setVisibility(View.VISIBLE);
        }
        else if (title.equals("企业理财")) {
            ywglNoTextQylc.setVisibility(View.VISIBLE);
        }
        else if (title.equals("信用贷款")) {
            ywglNoTextXydk.setVisibility(View.VISIBLE);
        }
        else if (title.equals("过桥短融")) {
            ywglNoTextGqdr.setVisibility(View.VISIBLE);
        }
        else if (title.equals("三板转让")) {
            ywglNoTextSbzr.setVisibility(View.VISIBLE);
        }
        else if (title.equals("并购转让")) {
            ywglNoTextBgzr.setVisibility(View.VISIBLE);
        }
        else if (title.equals("定向增发")) {
            ywglNoTextDxzf.setVisibility(View.VISIBLE);
        }
        else if (title.equals("杠杆配资")) {
            ywglNoTextGgpz.setVisibility(View.VISIBLE);
        }
    }


    @OnClick({R.id.ywgl_no_image_fh, R.id.ywgl_no_ll_gpzy, R.id.ywgl_no_ll_dzjc, R.id.ywgl_no_ll_qylc, R.id.ywgl_no_ll_xydk, R.id.ywgl_no_ll_gqdr, R.id.ywgl_no_ll_sbzr, R.id.ywgl_no_ll_bgzr, R.id.ywgl_no_ll_dxzf, R.id.ywgl_no_ll_ggpz, R.id.ywgl_no_button_tj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ywgl_no_image_fh:
                finish();
                break;
            case R.id.ywgl_no_ll_gpzy:
                goToContent("股票质押");
                break;
            case R.id.ywgl_no_ll_dzjc:
                goToContent("大宗减持");
                break;
            case R.id.ywgl_no_ll_qylc:
                goToContent("企业理财");
                break;
            case R.id.ywgl_no_ll_xydk:
                goToContent("信用贷款");
                break;
            case R.id.ywgl_no_ll_gqdr:
                goToContent("过桥短融");
                break;
            case R.id.ywgl_no_ll_sbzr:
                goToContent("三板转让");
                break;
            case R.id.ywgl_no_ll_bgzr:
                goToContent("并购转让");
                break;
            case R.id.ywgl_no_ll_dxzf:
                goToContent("定向增发");
                break;
            case R.id.ywgl_no_ll_ggpz:
                goToContent("杠杆配资");
                break;
            case R.id.ywgl_no_button_tj:
                requestYWGL();
                break;
        }
    }

    private void goToContent(String name) {
        int id = 0;
        String title = "";
        Intent intent = new Intent(this, YWXQActivity.class);
        for (IndexBean.DataBean.BusinessBean business : SampleApplicationLike.getIndexBeanInstance().getData().getBusiness()) {
            if (business.getTitle().equals(name)) {
                id = business.getId();
                title = business.getTitle();
            }
        }
        intent.putExtra("id", id);
        intent.putExtra("title", title);
        startActivity(intent);
    }



    /**
     * 提交业务管理
     */
    private void requestYWGL() {
        String id_info = "";
        if (ywglNoCheckboxGpzz.isChecked()){
            String id = "";
            for (IndexBean.DataBean.BusinessBean business : SampleApplicationLike.getIndexBeanInstance().getData().getBusiness()) {
                if (business.getTitle().equals("股票增值")) {
                    id = business.getId()+"";
                }
            }
            id_info = id_info + id + ",";
        }

        if (ywglNoCheckboxSzgl.isChecked()){
            String id = "";
            for (IndexBean.DataBean.BusinessBean business : SampleApplicationLike.getIndexBeanInstance().getData().getBusiness()) {
                if (business.getTitle().equals("市值管理方案定制")) {
                    id = business.getId() +"";
                }
            }
            id_info = id_info + id + ",";
        }

        if (ywglNoCheckboxFhbs.isChecked()){
            String id = "";
            for (IndexBean.DataBean.BusinessBean business : SampleApplicationLike.getIndexBeanInstance().getData().getBusiness()) {
                if (business.getTitle().equals("分红避税")) {
                    id = business.getId() +"";
                }
            }
            id_info = id_info + id + ",";
        }

        if (ywglNoCheckboxKjhz.isChecked()){
            String id = "";
            for (IndexBean.DataBean.BusinessBean business : SampleApplicationLike.getIndexBeanInstance().getData().getBusiness()) {
                if (business.getTitle().equals("可交换债")) {
                    id = business.getId() +"";
                }
            }
            id_info = id_info + id + ",";
        }


        if (!TextUtils.isEmpty(id_info)){
            HashMap<String,String > map = new HashMap<>();
            map.put("code", HttpUtil.getParam());
            map.put("users_id",SampleApplicationLike.getUserloginInstance().getId()+"");
            map.put("business_id",id_info);
            map.put("describe","");

            HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_YWBQSC, map, new Response.Listener() {
                @Override
                public void onResponse(Object response) {
                    ResultBean bean = (ResultBean) response;
                    if (bean.getResult() == 1){
                        startActivity(new Intent(YWGLNOActivity.this,YWGLYESActivity.class));
                        finish();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

        }else{
            startActivity(new Intent(YWGLNOActivity.this,YWGLYESActivity.class));
            finish();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        hasIntent();
    }
}
